package io.renren.modules.nfy.controller;

import io.renren.common.utils.Pages;
import io.renren.common.utils.ResponseModel;
import io.renren.modules.file.entity.FileEntity;
import io.renren.modules.file.service.FileUploadService;
import io.renren.modules.nfy.entity.ProductEntity;
import io.renren.modules.nfy.service.ProductService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 宁封窑-产品表
 *
 * @author xuewenjun
 * @email xwj@kafukeji.cn
 * @date 2023-05-25 10:37:58
 */
@RestController
@RequestMapping("nfy/product")
@Api(tags = "nfy-product")
public class ProductController extends AbstractController {
    @Autowired
    private ProductService productService;



    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public ResponseModel<Pages<ProductEntity>> list(@RequestBody ProductEntity product) {
        Pages<ProductEntity> page = productService.queryPage(product);

        return ResponseModel.of(page);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public ResponseModel<List<ProductEntity>> listAll(@RequestBody ProductEntity product) {
        List<ProductEntity> list = productService.listAll(product);
        return ResponseModel.of(list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    public ResponseModel<ProductEntity> info(@PathVariable("id") Long id) {
        ProductEntity product = productService.getById(id);

        return ResponseModel.of(product);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("新增")
    public ResponseModel<ProductEntity> save(@RequestBody ProductEntity product) {
        Date now = new Date();
        if (product.getId() == null) {
            product.setCreateTime(now);
            product.setCreateUser(getUser().getUsername());
            productService.doSave(product);
        } else {
            product.setUpdateTime(now);
            product.setUpdateUser(getUser().getUsername());
            productService.updateById(product);
        }

        return ResponseModel.of(product);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseModel<ProductEntity> update(@RequestBody ProductEntity product) {
        productService.updateById(product);

        return ResponseModel.of(product);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除或批量删除")
    public ResponseModel delete(@RequestBody Long[] ids) {
        productService.removeByIds(Arrays.asList(ids));

        return ResponseModel.ok();
    }

    @PostMapping("/uploadExcel")
    @ApiOperation("导入数据")
    public ResponseModel uploadExcel(@RequestParam("file") MultipartFile file) throws Exception {
        productService.uploadExcel(file, getUser());
        return ResponseModel.ok();
    }

    @PostMapping("/uploadFile")
    @ApiOperation("导入图片或zip")
    public ResponseModel uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("xtlx") String xtlx) {

        String type = file.getContentType();
        System.out.println(file.getOriginalFilename() + "___" + file.getContentType());
        String rs = null;
        // 上传的是图片
        if(type.startsWith("image/")) {
           rs = productService.saveImage(file, xtlx);
        } else {
            rs = productService.uploadZip(file, xtlx);
        }

        return ResponseModel.of(rs);
    }

}
